//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created Modif: 13/03/2005
// Last Modif: 13/04/2005


// Global vars for module
var MOD_CLDOCS = {
  editdoc:-1,
  editdocs_call_back:show_editdocs
}


// if no doc specified, select most interesting document used 
function seldocfirstused() {
  var cl_iter= new filteredclaims_iterator();   
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    if( IsClaimInFilter(cl)) { 
      if(GetClaimDoc_X(cl)) {
	return GetClaimDoc_X(cl);
      }
      if(GetClaimDoc_Y(cl)) {
	return GetClaimDoc_Y(cl); 
      }
      if(GetClaimDoc_A(cl)) {
	return GetClaimDoc_A(cl);
      }
    }
    cl_iter.next();
  }
  return "";
}

MOD_CLDOCS.updatenewpn=function(title,pd,pri) {
  document.getElementById("newdoctitle").value=MOD_SPN.title;
  document.getElementById("newdocpd").value=MOD_SPN.pd; 
  document.getElementById("newdocpri").value=MOD_SPN.pri;
}

MOD_CLDOCS.updateeditpn=function(title,pd,pri) {
  document.getElementById("editdoctitle").value=title;
  document.getElementById("editdocpd").value=pd;
  document.getElementById("editdocpri").value=pri;
}

// add new doc PN
MOD_CLDOCS.onchangenewpn=function(pn) {
  if(pn) {
    pn=DocDBNormalize(pn);
    document.getElementById("newdocpn").value=pn;
    if(FindDocDB(pn) && GetDocDBTitle(pn) && GetDocDBTitle(pn)!= DocDBNormalize(pn)) {
      //document.getElementById("addbutton").value="<u>A</u>dd/Upd";
      document.getElementById("newdoctitle").value=GetDocDBTitle(pn);
      document.getElementById("newdocpd").value=GetDocDBPD(pn);
      document.getElementById("newdocpri").value=GetDocDBPRI(pn);
    } else {   
      checkespacenet(pn,MOD_CLDOCS.updatenewpn);
    } 
  }
}

// used to change from TMP doc to final XP doc 
MOD_CLDOCS.onchangedocpn = function(doc,oldpn,newpn) {
  newpn=DocDBNormalize(newpn);
  if(confirm("Replace all references to "+oldpn+" with "+newpn+" ?")) { 
    SetDocPN(doc,newpn);
    var cl_iter = new claim_iterator();
    while(!cl_iter.end()) { 
      // Update claim values
      var cl=cl_iter.getValue();
      // Update doc strategy 
      if(GetClaimDocStrat(cl,oldpn)) SetClaimDocStrat(cl,GetClaimDocStrat(cl,oldpn),newpn);
      // Update doc values
      RenameClaimDocVal(cl,oldpn,newpn);
      // update feature refs
      var tf_iter= new claim_features_iterator(cl);
      while(!tf_iter.end()) {
	tf_iter.setPN(oldpn,newpn);
        tf_iter.next();
      }
      cl_iter.next();
    }
    RenameGroupDoc(oldpn,newpn); // rename group doc references
    if(MOD_CLDOCS.is_tmpdoc(oldpn) || confirm("Apply to document database ?")) { 
      SetDocDBPN(oldpn,newpn);
    }
  }
}


MOD_CLDOCS.askdeldocs = function(idx) {
  var changes=0;
  var pn=GetDocPN(idx);
  if(confirm("Delete document "+pn+ " ?")) {
    for(var cl=1; cl<=GetClaimSetLength(); cl++) { // delete strategy references to doc
      if(GetClaimDoc_X(cl) == pn) {
	SetClaimDoc_X(cl,"");changes++;
      } else if(GetClaimDoc_Y(cl) == pn) {
	SetClaimDoc_Y(cl,"");changes++;
      } else if(GetClaimDoc_A(cl) == pn) {
	SetClaimDoc_A(cl,"");changes++;
      } if(GetClaimDoc_B(cl) == pn) {
	SetClaimDoc_B(cl,"");changes++;
      }
      if(changes) {
	updatefromdocs(cl);
      }
    }    
    DeleteDoc(idx);
    MOD_CLDOCS.editdocs_call_back();
  }
}

MOD_CLDOCS.adddoc = function() { 
  var pn=DocDBNormalize(document.getElementById("newdocpn").value);
  if(pn) {    
    AddDoc(document.getElementById("newdocpn").value,
	   document.getElementById("newdoctitle").value,
	   document.getElementById("newdocpd").value,
	   document.getElementById("newdocpri").value,
	   document.getElementById("newD").checked); 
  }

  MOD_CLDOCS.editdocs_call_back();
  //edit_docs();	
}

MOD_CLDOCS.applyeditdoc=function(doc,oldpn) {
  var newpn=DocDBNormalize(document.getElementById("editdocpn").value);
  if(newpn!=oldpn) {
    MOD_CLDOCS.onchangedocpn(doc,oldpn,newpn);
  }
  
  AddDoc(document.getElementById("editdocpn").value,
	 document.getElementById("editdoctitle").value,
	 document.getElementById("editdocpd").value,
	 document.getElementById("editdocpri").value,
	 document.getElementById("editD").checked);
  MOD_CLDOCS.exiteditdoc();
}


MOD_CLDOCS.is_tmpdoc = function(pn) {
  return pn.toUpperCase().indexOf("TMP") >=0;
}

MOD_CLDOCS.onchangetitle = function(obj) {
  obj.value=trim(obj.value);
}

MOD_CLDOCS.onchangedate = function(obj) {
   obj.value=normalizeDate(trim(obj.value));
}

MOD_CLDOCS.onchangedocnotcited=function(doc,pn,obj) {
  if(obj.checked) {
    SetDocNotCited(doc,"yes");
    SetDocCitedNum(pn,"");
  } else {
    SetDocNotCited(doc,"");
  }	 	 
  MOD_CLDOCS.editdocs_call_back();
}

MOD_CLDOCS.doselectodoc= function(id,pn) {
  var  val = html_selected_iter(id);
  if(SetDocIdx(pn,val)) {
    SetDocCitedNum(pn,val);
    ReorderDocs();
  }
  MOD_CLDOCS.exiteditdoc();
}

MOD_CLDOCS.oneditdoc= function(doc) {
  MOD_CLDOCS.editdoc=doc;
  MOD_CLDOCS.editdocs_call_back();
}

MOD_CLDOCS.exiteditdoc = function() {
  MOD_CLDOCS.editdoc=-1;
  MOD_CLDOCS.editdocs_call_back();
}

MOD_CLDOCS.edit_doc_entry= function(dociter) {
  var docord_iter=new range_iterator(1, GetDocsLength());   
  var html="<TD>";
  html+=html_selection_iter("odoc"+dociter.getValue(),
			    docord_iter,
			    dociter.getCitedNum(),
			    "MOD_CLDOCS.doselectodoc(\"odoc"+dociter.getValue()+"\",\""+dociter.getPN()+"\")",""," ");
  html+="</TD>";
  html+="<TD><input type=checkbox name=T  "+(dociter.isNotCited()? "  checked ":"")+"></TD>";
  html+="<TD><input type=text name=editdocpn size=12 value='"+dociter.getPN()+"'";
  html+="onchange='checkespacenet(this.value,MOD_CLDOCS.updateeditpn);' </TD>";

  html+="<TD><input type=checkbox class='checkdoc' name=editD  "+(dociter.isD()? "  checked ":"")+">";
  html+="<input type=checkbox class='checkdoc' name=P disabled "+(dociter.isP()? " checked ":"")+">";
  html+="<input type=checkbox class='checkdoc' name=E disabled "+(dociter.isE()? " checked ":"")+"></TD>";
 
  html+="<TD><input type=text name=editdoctitle size=64 value='"+dociter.getTitle()+
  "' onchange='MOD_CLDOCS.onchangetitle(this)'></TD>";
  html+="<TD><input type=text name=editdocpd size=8 value='"+dociter.getPD()+
  "' onchange='MOD_CLDOCS.onchangedate(this)'></TD>";
  html+="<TD><input type=text name=editdocpri size=8 value='"+dociter.getPri()+
  "' onchange='MOD_CLDOCS.onchangedate(this)'></TD>";
  
  html+="<TD><BUTTON  onclick='MOD_CLDOCS.applyeditdoc("+dociter.getValue()+",\""+dociter.getPN()+"\");'";
  html+=" ACCESSKEY='A'>"+html_img(ok_img)+"</button></TD>";   
  html+="<TD><BUTTON  onclick='MOD_CLDOCS.exiteditdoc();' CLASS='ACTBUT'>"+html_img(cancel_img)+"</button></TD>";
 
  return html;
}

MOD_CLDOCS.add_doc_entry= function() {
  var html="<TD></TD>";
  html+="<TD></TD>";
  html+="<TD><input type=text id=newdocpn size=12 value='' onChange='MOD_CLDOCS.onchangenewpn(this.value);' ";
  //html+="onKeyPress='doccheckEnter(event,this.value);' tabindex=1></TD>";
  html+=" tabindex=1></TD>";
  html+="<TD><input type=checkbox class='checkdoc' name=newD tabindex=10><input type=checkbox class='checkdoc' id=newP disabled><input type=checkbox class='checkdoc' id=newE disabled></TD> ";
  html+="<TD><input type=text id=newdoctitle size=64 value='' tabindex=2 onchange='MOD_CLDOCS.onchangetitle(this)'></TD>";
  html+="<TD><input type=text id=newdocpd size=8 value=''  tabindex=3 onchange='MOD_CLDOCS.onchangedate(this)'></TD>";
  html+="<TD><input type=text id=newdocpri size=8 value='' tabindex=4 onchange='MOD_CLDOCS.onchangedate(this)'></TD>";
  html+="<TD colspan=2><BUTTON TYPE=Button onclick='MOD_CLDOCS.adddoc();' id=addbutton  ACCESSKEY='A'>"+html_img(add_img)+"</button></TD>";
  return html;
}


MOD_CLDOCS.onsortdocs= function() {
  ReorderDocs();
  MOD_CLDOCS.editdocs_call_back();
}

MOD_CLDOCS.onclickondoc = function(doc) {
  edit_features(doc);
}

//
// Public methods
// 

function edit_docs(callback) {
  InitDocPNS();
  var html="";
  var tabidx=1;
  if(callback) {
    MOD_CLDOCS.editdocs_call_back=callback;
  }
  html+="<TABLE border=1 width=100%>";
  html+="<TR>";
  //html+="<TH>"+html_whatis("<input type=button onclick='onsortdocs()' value='D#'>","Document number")+"</TH>";
  html+="<TH>"+html_whatis("D#","Document number")+"</TH>";
  html+="<TH>"+html_whatis("H","Hide document")+"</TH>";
  html+="<TH>"+html_whatis("PN","Publication Number")+"</TH>";
  html+="<TH>"+html_whatis("D ","D (cited) document Flag");
  html+=html_whatis("P ","P Document Flag");
  html+=html_whatis("E","E Document Flag")+"</TH>";
  html+="<TH> Title </TH>";
  html+="<TH>"+html_whatis("PD","Publication Date")+"</TH>";
  html+="<TH>"+html_whatis("Pri","Priority Date")+"</TH></TR>";
  var doc_iter = new doc_iterator("","","ALL");
  var pn,doc;
  while(!doc_iter.end()) {
    pn=doc_iter.getPN();
    doc=doc_iter.getValue();
    //docord_iter.reset();
    if(doc == MOD_CLDOCS.editdoc) {
      html+="<TR>"+MOD_CLDOCS.edit_doc_entry(doc_iter)+"</TR>";
    } else {
      html+="<TR>";
      if(doc_iter.isNotCited()) {
	html+="<TD align=center> ? </TD>";
      } else {
	if(doc_iter.getCitedVer() && doc_iter.getCitedVer()< GetClaimSetVersion()) {
	  html+="<TD align=center><b>"+html_whatis((parseInt(doc_iter.getCitedNum()) || "?"),
						(doc_iter.getCitedNum() && "First used in "+doc_iter.getCitedVer())
						|| "Not used yet")+"</b>";
	} else {
	  html+="<TD align=center>"+html_whatis((parseInt(doc_iter.getCitedNum()) || "?"),
					      (doc_iter.getCitedNum() && "First used in "+doc_iter.getCitedVer())
					      || "Not used yet");
	}
	html+="</TD>";
      }
      html+="<TD align=center><input type=checkbox class='checkdoc' name=T  "+(doc_iter.isNotCited()? "  checked ":"");
      html+=" onClick='MOD_CLDOCS.onchangedocnotcited("+doc+",\""+pn+"\",this);'></TD>";    
      if(doc_iter.isNotCited()) {
	html+="<TD align=center>"+pn+"</TD>";
      } else {
	html+="<TD align=center><input type=button CLASS='DOC' onClick='MOD_CLDOCS.onclickondoc(\""+pn+"\")' ";
	html+=" value='"+pn+"'></TD>";
      }
      html+="<TD><input type=checkbox class='checkdoc' name=D disabled "+(doc_iter.isD()? "  checked ":"")+">";
      html+="<input type=checkbox class='checkdoc' name=P disabled "+(doc_iter.isP()? " checked ":"")+">";
      html+="<input type=checkbox class='checkdoc' name=E disabled "+(doc_iter.isE()? " checked ":"")+"></TD>";
      html+="<TD><textarea style='overflow: hidden;'  readonly rows=1 cols=40,this.value);'>";
      html+=doc_iter.getTitle()+"</TEXTAREA>";
      html+="</TD><TD align=center>"+doc_iter.getPD()+"</TD><TD align=center>"+doc_iter.getPri()+"</TD>";
      html+="<TD><Button onClick='MOD_CLDOCS.askdeldocs("+doc+");' class='FULLW'>"+html_img(trash_img)+"</button> </TD>";
      html+="<TD><button onclick='MOD_CLDOCS.oneditdoc("+doc+");' class='FULLW'>"+html_img(edit_img)+"</button></TD></TR>";
    }
    doc_iter.next();
  }
  html+="<TR>"+MOD_CLDOCS.add_doc_entry()+"</TR>";
  html+="</TABLE>";
  return html;
}

MOD_CLDOCS.refreshnet=function(pn) {
  checkespacenet(pn,function(title,pd,pri) { UpdateDocDB(pn,title,pd,pri)});
}

MOD_CLDOCS.refreshdocs=function() {
  CaesarImportAllcitations();
  var doc_iter=new doc_iterator("","","ALL");
  while(!doc_iter.end()) {
    MOD_CLDOCS.refreshnet(doc_iter.getPN());
    doc_iter.next();
  }  
  show_editdocs();
}



function show_editdocs() {
  ClaimToolEditSection("Edit Documents",
		       edit_docs(show_editdocs),
		       "show_editdocs()",
		       "MOD_CLDOCS.refreshdocs()",
		       refresh_img);  
}





  
